#include "../codec.h"
using namespace pubsub;
ParseResult pubsub::parseMessage(yhaida::Buffer *buff,
                                 std::string *cmd,
                                 std::string *topic,
                                 std::string *content)
{
    ParseResult result = kError;
    const char *crlf = buff->findCRLF();
    if (crlf)
    {
        const char *space = std::find(buff->peek(), crlf, ' ');
        if (space != crlf)
        {
            cmd->assign(buff->peek(), space);
            topic->assign(space + 1, crlf);
            if (*cmd == "pub")
            {
                //下一个换行符
                const char *start = crlf + 2;
                crlf = buff->findCRLF(start);
                if (crlf)
                {
                    content->assign(start, crlf);
                    buff->retrieveUntil(crlf + 2);
                    result = kSuccess;
                }
                else
                {
                    result = kContinue;
                }
            }
            else //没有对应的指令
            {
                buff->retrieveUntil(crlf + 2);
                result = kSuccess;
            }
        }
        else
        {
            result = kError;
        }
    }
    else
    {
        result = kContinue;
    }
    return result;
}